<?php
namespace app\admin\controller;
use think\Request;
use think\Controller;
use think\Db;
use think\file;
use app\admin\controller\Base;

class Ad extends Base
{
    public function ads()
    {
        $list =Db::table('ad')
            ->alias('a')
            ->join('position p','a.position_id = p.position_id','LEFT')
            ->select(); 
        $this->assign('list',$list);
        return $this->fetch();
    }
    //   public function up(Request $request){
    //     $file = $request->file('file');
    //     if (empty($file)) {
    //         $this->error('请选择上传文件');
    //     }
    //        $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
    //     if ($info) {
    //         $this->success('文件上传成功：' . $info->getRealPath());
    //     } else {
    //         $this->error($file->getError());
    //     }

    // }

    public function add()
    {
       $data=input('post.');

       $file=request()->file('ad_img');
        $info=$file->move(ROOT_PATH.'public'.DS.'uploads');
        // var_dump($info);
        $filename=$info->getSaveName();
        $data['ad_img']=$filename;

        $result=Db('ad')->insert($data);
        if($result){
            $this->success('添加广告成功','ad/ads');
        }else{
            $this->error('添加广告失败','ad/addads');
        }
    }
    
    public function addads()
    {

         $result=Db('position')->select();
           $this->assign('p',$result);
        return $this->fetch();
    }

    public function add_po()
    {
        $data=input('post.');
        $result=Db('position')->insert($data);
        if($result){
            $this->success('添加广告位成功','ad/ad_position');
        }else{
            $this->error('添加广告位失败','ad/addads_position');
        }



    }
    public function ad_position()
    {   

        $list=Db('position')->select();

        $this->assign('list',$list);
        return $this->fetch();
    }

    public function addads_position()
    {
        return $this->fetch();
    }
}